const COS = require('cos-nodejs-sdk-v5')
const fs=require('node:fs')
const {SecretId,SecretKey,Bucket,Region}=require('../env')

const ploads=(filename)=>{
    return new Promise((resolve,reject)=>{
        const filePath='uploads/'+filename
        const cos = new COS({
            SecretId: SecretId, 
            SecretKey: SecretKey, 
            Protocol:'https:'
        });
        cos.putObject({
            Bucket:Bucket,
            Region:Region,
            Key:filename,
            Body: fs.createReadStream(filePath), // 上传文件对象
        }, function (err, data) {
            if(data){
                resolve("https://"+data.Location)
            }else{
                reject(err)
            }
        })
    })
}

module.exports={
    ploads
}